package com.summer.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetHtmlTest {
	
	public static void main(String[] args) {
//		String html = getHtmlSource("https://www.oschina.net/",60000,"UTF-8","");
		String html = getHtmlSource("http://www.chinaznj.com/",600000,"UTF-8","");
		List<String> list = parseHtml(html);
		for(String url : list){
			System.out.println(url);
//			getHtmlSource(url);
		}
		
	}
	
	
	public static String getHtmlSource(String url, int Timeout, String encoding, String defaultValue) {
		HttpURLConnection connection = null;
		InputStreamReader reader = null;
		BufferedReader l_reader = null;
		try {
			if (encoding == null) {
				encoding = "gbk";
			}
			URL dataUrl = new URL(url);
			connection = (HttpURLConnection) dataUrl.openConnection();
			// 设置超时时间
			connection.setConnectTimeout(Timeout);
			connection.setReadTimeout(Timeout);
//			connection.addRequestProperty("User-Agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36");
			connection.connect();
			// InputStream l_urlStream = connection.getInputStream();
			reader = new InputStreamReader(connection.getInputStream(),encoding);
			// 读取请求结果
			// BufferedReader l_reader = new BufferedReader(new
			// InputStreamReader( connection.getInputStream(), encoding));
			l_reader = new BufferedReader(reader);

			String sCurrentLine = "";
			String sTotalString = "";
			while ((sCurrentLine = l_reader.readLine()) != null) {
				sTotalString = sTotalString + sCurrentLine;
			}
			return sTotalString;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != l_reader) {
					l_reader.close();
				}
				if (null != reader) {
					reader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			connection.disconnect();
		}
		return defaultValue;
	}
	private static final String rexStr = "<div class=\"col-bottom\">([\\s\\S])*?</div>";
	private static final Pattern pattern = Pattern.compile(rexStr);
	
	private static final String lineRexStr = "href=\"([\\s\\S])*?\"";
	private static final Pattern linePattern = Pattern.compile(lineRexStr);
	
	public static List<String> parseHtml(String htmlStr){
		if (htmlStr==null || htmlStr.length()<=0) {
			return null;
		}
//		Matcher m = pattern.matcher(htmlStr);
		Matcher m = linePattern.matcher(htmlStr);

		List<String> list = new ArrayList<>();
		while (m.find()) {
			String url = m.group();
			if(!url.contains("www.chinaznj.com"))
				continue;
			if(!list.contains(url))
				list.add(url);
        }
		
		return list;
	}
}
